package queries;

import java.rmi.ServerError;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import exception.ServerDownException;




public class SavedCommentsQueries 
{
	
	private final static String INSERT_SAVED_COMMENT_GIVEN_COMMENT_ID ="INSERT INTO saved_comments(LOGIN,COMMENT_ID) VALUES (?, ?)"; 
	
	private final static String VERIFY_LOGIN_COMMENT_ID="SELECT count( * ) AS CONTADOR FROM saved_comments WHERE LOGIN = ? AND COMMENT_ID = ?";
	
	
	
	public void saveComment(String login, int commentId, Connection session) throws ServerDownException
	{
		try
		{
			PreparedStatement ps = session.prepareStatement(INSERT_SAVED_COMMENT_GIVEN_COMMENT_ID);
			ps.setString(1, login);
			ps.setInt(2, commentId);
			ps.executeUpdate();
		}
		catch(Exception e)
		{
			throw new ServerDownException(e.getLocalizedMessage());
		}
		
	}
	
	public boolean getLoginSavedComment(String login, int commentId, Connection session) throws ServerDownException
	{
		try
		{
			boolean answer=false;
			
			PreparedStatement ps = session.prepareStatement(VERIFY_LOGIN_COMMENT_ID);
			ps.setString(1, login);
			ps.setInt(2,commentId);
			ResultSet rs=ps.executeQuery();
			rs.next();
			if(rs.getInt("CONTADOR")>0)
				return answer;
			else
				return !answer;
			
		}
		catch(Exception e)
		{
			throw new ServerDownException(e.getLocalizedMessage());
		}
	}
}
